package de.pharmacontrol.psa.beans;

import java.util.List;

import javax.ejb.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import de.pharmacontrol.pilot.entities.AI;
import de.pharmacontrol.psa.beans.interfaces.AiService;

@Singleton
public class AiServiceBean implements AiService {

	@PersistenceContext(unitName = "pilot")
	protected EntityManager em;

	@Override
	public AI findAiByName(String name) {
		try {
			return em.createQuery("SELECT a FROM AI a WHERE a.ai = :name", AI.class).setParameter("name", name).getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Override
	public List<AI> findAll() {
		TypedQuery<AI> query;
		try {
			query = em.createQuery("SELECT a FROM AI a ORDER BY a.ai DESC", AI.class);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return query.getResultList();
	}

	@Override
	public AI updateAi(AI ai) {
		return em.merge(ai);
	}

	@Override
	public AI addAi(AI ai) {
		// TODO Auto-generated method stub
		return null;
	}

}
